package com.hx.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 简单的 reentrantLock 分析  不带condition
 * @author minghuaChan
 * @since 2023/9/17 17:05
 */
public class SimpleReentrantLock {

    // 创建一个公平锁
     static final  ReentrantLock LOCK = new ReentrantLock(true);

    public static void main(String[] args) {


        Thread thread0 = new Thread(() ->{
            doSth(LOCK);
        });

        thread0.start();

        Thread thread1 = new Thread(() ->{
            doSth(LOCK);
        });

        thread1.start();


        Thread thread2 = new Thread(() ->{
            doSth(LOCK);
        });

        thread2.start();

    }


    public static void doSth(ReentrantLock lock) {
        // 锁住
        lock.lock();

        System.out.println(Thread.currentThread().getName()+"获取锁");


        // doSth
        System.out.println(Thread.currentThread().getName()+"业务处理中");

        // doSth
        System.out.println(Thread.currentThread().getName()+"业务处理完毕");

        // 释放锁
        lock.unlock();

        System.out.println(Thread.currentThread().getName()+"释放锁");
    }


}
